package cn.regexp.service.impl;

import cn.regexp.domain.OperateIntergralVo;
import cn.regexp.domain.OrderInfo;
import cn.regexp.mapper.OrderInfoMapper;
import cn.regexp.service.IOrderInfoService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class OrderInfoServiceImpl implements IOrderInfoService {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Override
    @Transactional
    public String refund(String orderNo) {
        OrderInfo orderInfo = orderInfoMapper.select(orderNo);
        OperateIntergralVo vo = new OperateIntergralVo();
        vo.setPk(orderInfo.getOrderNo());
        vo.setUserId(orderInfo.getUserId());
        vo.setValue(orderInfo.getIntergral());
        vo.setInfo("退款操作");

        Message<OperateIntergralVo> message = MessageBuilder.withPayload(vo).build();
        rocketMQTemplate.syncSend("tx_topic",message);
        orderInfoMapper.changeRefundStatus(orderNo,OrderInfo.STATUS_REFUND);

        return "退款成功";
    }
}
